package cn.wolfcode.luowowo.service.impl;


import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService{

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IRegionService regionService;

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public List<Destination> findByIdIn(List<String> ids) {
        return destinationRepository.findByIdIn(ids);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //条件
        Query query = new Query();
        //带上条件
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }
        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            //吐司的地方显示中国
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        //获取query
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(mongoTemplate,Destination.class,pageable,query);
    }

    @Override
    public List<Destination> getToats(String parentId) {
        //通过父id获取
        if(!StringUtils.hasLength(parentId)){
            //如果没有就返回空集合
            return Collections.emptyList();
        }
        List<Destination> list = new ArrayList<>();
        /*//广州
        Destination destination = this.get(parentId);
        list.add(destination);
        if(StringUtils.hasLength(destination.getParentId())){
            //广东
            Destination destination2 = this.get(destination.getParentId());
            list.add(destination2);
            if(StringUtils.hasLength(destination2.getParentId())){
                //中国
                Destination destination3 = this.get(destination2.getParentId());
                list.add(destination3);
            }
        }*/
        createToats(list,parentId);
        Collections.reverse(list);//集合元素反转
        return list;
    }

    @Override
    public List<Destination> queryByRegionIdForApi(String regionId) {
        //区分国内和国外
        List<Destination> destinations = new ArrayList<>();
        if("-1".equals(regionId)){
            //国内
            destinations = destinationRepository.findByParentName("中国");
        }else{
            //国外
            destinations = regionService.getDestByRegionId(regionId);
        }
        //获取儿子
        for (Destination destination : destinations) {
            Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            //获取子类
            List<Destination> children = destinationRepository.findByParentId(destination.getId(),pageable);
            destination.setChildren(children);
        }
        return destinations;
    }

    private void createToats(List<Destination> list ,String parentId){
        Destination destination = this.get(parentId);
        list.add(destination);
        if(StringUtils.hasLength(destination.getParentId())){
            createToats(list,destination.getParentId());
        }
    }

    @Override
    public void updateInfoById(String info, String id) {
        //通过id,获取目的地
        Destination destination = this.get(id);
        //设置目的地信息
        destination.setInfo(info);
        //更新
        this.update(destination);
    }
}

